#include "include.h"

#if HUART_DUMP_AUDIO
static huart_dump_cfg_t huart_dump_cfg;
ALIGNED(4)
static u8 sinetbl_test[128] = {   //32 samples  //sine_16k_500hz_dual正弦波测试数据
	0x00, 0x00, 0xFF, 0xFF, 0xF9, 0x18, 0xF9, 0x18, 0xFB, 0x30, 0xFB, 0x30,
	0x1F, 0x47, 0x1E, 0x47, 0x7F, 0x5A, 0x81, 0x5A, 0x72, 0x6A, 0x6F, 0x6A,
	0x3D, 0x76, 0x42, 0x76, 0x8F, 0x7D, 0x88, 0x7D, 0xFC, 0x7F, 0xFF, 0x7F,
	0x8D, 0x7D, 0x88, 0x7D, 0x40, 0x76, 0x44, 0x76, 0x6F, 0x6A, 0x6D, 0x6A,
	0x82, 0x5A, 0x81, 0x5A, 0x1D, 0x47, 0x1E, 0x47, 0xFB, 0x30, 0xFB, 0x30,
	0xFA, 0x18, 0xFA, 0x18, 0xFE, 0xFF, 0xFF, 0xFF, 0x0A, 0xE7, 0x07, 0xE7,
	0x03, 0xCF, 0x04, 0xCF, 0xE2, 0xB8, 0xE4, 0xB8, 0x7F, 0xA5, 0x7B, 0xA5,
	0x92, 0x95, 0x95, 0x95, 0xBD, 0x89, 0xBD, 0x89, 0x79, 0x82, 0x76, 0x82,
	0x01, 0x80, 0x00, 0x80, 0x79, 0x82, 0x78, 0x82, 0xBC, 0x89, 0xBA, 0x89,
	0x95, 0x95, 0x99, 0x95, 0x7A, 0xA5, 0x76, 0xA5, 0xE7, 0xB8, 0xE9, 0xB8,
	0x01, 0xCF, 0x01, 0xCF, 0x09, 0xE7, 0x0A, 0xE7
};

ALIGNED(4)
u8 huart_buffer_rx[128];

static dump_protocol dump;

AT(.text.dumpbuf)
void dump_buf_init(u8 *buf, int buf_len, int file_toal,void (*putbuf)(void*buf,u32 len), void (*wait)(void))
{
    int i, len_min;
    if (NULL == buf ||  file_toal < 1) {
        dump_printf("dump_var_init param ERR\n");
        return;
    }
    len_min = (file_toal + 1) * HEAD_LEN + 4;
    if(buf_len <  len_min) {
        dump_printf("!!!Error, dump_var_init , buf_len = %d, is to small\n",len_min);
        return;
    }
    memset(buf, 0x00, buf_len);
    memset(&dump, 0x00, sizeof(dump_protocol));
    dump.head_buf = buf + file_toal * HEAD_LEN;
    dump.data_buf = buf + (file_toal + 1) * HEAD_LEN;
    dump.data_buf_len = buf_len - (file_toal + 1) * HEAD_LEN;
    dump.tx_len = dump.data_buf_len + HEAD_LEN;
    //head
    for(i = 0; i < file_toal; i++) {
        dump.head[i] = (dump_head*)(buf + i * HEAD_LEN);
        dump.head[i]->tag = HEAD_TAG;  //UT67
        dump.head[i]->pkt_len = dump.data_buf_len;
        dump.head[i]->type = (u8)i & 0x0F;
    }
    dump.putbuf = putbuf;
    dump.wait_put_finish = wait;
    dump_printf("sizeof(dump_head) = %d, dump.data_buf_len = %d\n",HEAD_LEN,dump.data_buf_len);
    dump_printf("-->dump_buf_init ok\n");
}

AT(.com_text.dumpbuf.crc)
static u16 calc_sum(void *buf, uint len)
{
    u16 sum = 0;
    u8* buf8 = (u8*)buf;
    for(uint i = 0; i < len; i++) {
        sum += buf8[i];
    }
    return sum;
}

AT(.com_text.dumpbuf.cststr)
const char dump_str_buf_err[] = "dump_buf len err(%d,%d)\n";

AT(.com_text.dumpbuf)
void dump_putbuf(u8 *buf,  int buf_len, u8 file_idx)
{
    if(buf_len > dump.data_buf_len) {
        dump_printf(dump_str_buf_err,buf_len,dump.data_buf_len);
        return;
    }
    dump.wait_put_finish();  //等待上一次发送完成
    memcpy(dump.data_buf, buf, buf_len);
    dump.head[file_idx]->pkt_cnt++;
    dump.head[file_idx]->pkt_len = buf_len;
    dump.head[file_idx]->crc = calc_sum(dump.data_buf, buf_len);
    dump.head[file_idx]->head_sum = calc_sum(&(dump.head[file_idx]->pkt_cnt),9);

    memcpy(dump.head_buf, dump.head[file_idx], HEAD_LEN);
    dump.putbuf(dump.head_buf, buf_len + HEAD_LEN);
}

static u8 dump_cache_buf[DUMP_BUF_LEN];

AT(.text.huart_dump)
void huart_dump_init(u8 sample_rate, u16 samples)
{
    huart_dump_cfg.baud_rate = 8000000;
    huart_dump_cfg.buf = huart_buffer_rx;
    huart_dump_cfg.buf_size = DUMP_BUF_LEN;
    huart_dump_cfg.isr_rx_en = 0;
    huart_dump_cfg.tx_port = HUART_TR_PE6;
    huart_dump_cfg.rx_port = HUART_TR_PE7;

    huart_init_do(huart_dump_cfg.tx_port, huart_dump_cfg.rx_port, huart_dump_cfg.baud_rate, huart_dump_cfg.buf, huart_dump_cfg.buf_size, huart_dump_cfg.isr_rx_en);

    dump_buf_init(dump_cache_buf, DUMP_BUF_LEN, 2, huart_putcs, huart_wait_txdone);
}


AT(.com_text.huart_dump)
void huart_dump_audio_input(u8 *ptr, u32 samples, int ch_mode, void *params)
{
    dump_putbuf(ptr, 2*samples, 0);  //dump数据存到文件0
    if (huart_dump_cfg.callback) {
        huart_dump_cfg.callback(ptr, samples, ch_mode, params);
    }
}

AT(.text.huart_dump)
void huart_dump_audio_output_callback_set(audio_callback_t callback)
{
    huart_dump_cfg.callback = callback;
}

AT(.text.huart_dump)
void dump_test(void)
{
    dump_putbuf(sinetbl_test,128,0);  //导出的数据会保存到文件0
    dump_putbuf(sinetbl_test,128,1);  //导出的数据会保存到文件1
}

void bt_uart_exit(void)
{
}

#endif
